﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreesenseSubAPI.Core
{
    public class BackLight
    {
        public System.IO.Ports.SerialPort serialPort1;
        public BackLight(string comStr)
        {
            try
            {
                serialPort1 = new System.IO.Ports.SerialPort(comStr);
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.WriteTimeout = 3000;
                serialPort1.StopBits = System.IO.Ports.StopBits.One;
                serialPort1.Open();
                SetTiggerMode(1,false);
            }
            catch (Exception ex)
            {
                BpjbCommon.Net.Core.BpEventHandler.ErrHandler(string.Format("错误消息：背光源串口 {0} 连接失败！\r\n解决方法:\r\n1.检查设备是否运行正常。\r\n2.检查通信串口是否配置正确。\n", comStr) + ex.Message, BpjbCommon.Net.Core.ErrType.Warning);
            }
        }
        #region 威朗21栋背光
        private byte rs232_write(byte[] str)
        {
            if (serialPort1 != null && serialPort1.IsOpen)
                serialPort1.Write(str, 0, str.Length);
            else
            {
                throw new ArgumentException("串口连接失败！");
            }
            return 0;
        }
        /// <summary>
        /// 打开通道光源
        /// </summary>
        /// <param name="sCH">0x01-1通道，0x02-2通道</param>
        /// <returns></returns>
        public byte OpenPower(byte sCH)
        {
            //帧头   命令      帧长              帧长校验      数据    异或校验和
            //1字节  1字节     1字节             1 字节        N 字节  1 字节
            //0xFE   参见定义  数据长度，最大50  字节帧长取反  无        数据校验和
            //FE     0B        01                FE            01      FE
            //                 01为通道1                       数据01
            byte temp;
            byte no_of_data = 6;
            byte[] str = new byte[no_of_data];
            str[0] = 0xFE;
            str[1] = 0x0B;
            str[2] = 0x01;
            temp = (byte)(~(str[2]));
            str[3] = 0xFE;
            str[4] = sCH;
            str[5] = 0xFE;
            if (rs232_write(str) == 0)
                return 0;
            else
                return 1;
        }
        public byte ClosePower(byte sCH)
        {
            //帧头   命令      帧长              帧长校验      数据    异或校验和
            //1字节  1字节     1字节             1 字节        N 字节  1 字节
            //0xFE   参见定义  数据长度，最大50  字节帧长取反  无        数据校验和
            //FE     0C        01                FE            01      FE
            //                 01为通道1                       数据01
            byte temp;
            byte no_of_data = 6;
            byte[] str = new byte[no_of_data];
            str[0] = 0xFE;
            str[1] = 0x0C;
            str[2] = 1;
            temp = (byte)(~(str[2]));
            str[3] = 0xFE;
            str[4] = sCH;
            str[5] = 0xFE;
            if (rs232_write(str) == 0)
                return 0;
            else
                return 1;
        }
        /// <summary>
        /// 设置背光源亮度等级
        /// </summary>
        /// <param name="sCH">通道号为1</param>
        /// <param name="sIn">亮度：0-255</param>
        /// <returns></returns>
        public byte SetGrade(byte sCH, byte sIn)
        {
            byte temp;
            byte sum = 0;
            byte[] COMframedata = new byte[256];
            sum = 0;
            COMframedata[0] = 0xFE;
            COMframedata[1] = 0x0E;
            COMframedata[2] = 2;
            temp = (byte)(~(COMframedata[2]));
            COMframedata[3] = temp;
            COMframedata[4] = 1;
            COMframedata[5] = sIn;//亮度
            sum ^= COMframedata[4];
            sum ^= COMframedata[5];
            COMframedata[6] = sum;
            if (rs232_write(COMframedata) == 0)
                return 0;
            else
                return 1;
        }
        /// <summary>
        /// 设置触发模式，只有关闭触发模式才能控制亮度
        /// </summary>
        /// <param name="sCH">通道号1</param>
        /// <param name="sIn">开:true，关：false</param>
        /// <returns></returns>
        public byte SetTiggerMode(byte sCH, bool sIn)
        {
            byte temp;

            byte[] COMframedata = new byte[256];
            COMframedata[0] = 0xFE;
            COMframedata[1] = 0x84;
            COMframedata[2] = 1;
            temp = (byte)(~(COMframedata[2]));
            COMframedata[3] = temp;
            if (sIn)
                COMframedata[4] = 1;
            else
                COMframedata[4] = 0;
            COMframedata[5] ^= COMframedata[4];
            if (rs232_write(COMframedata) == 0)
                return 0;
            else
                return 1;
        }
        #endregion 背光
    }
}
